package com.empire.module.member.dal.mysql.mapconfig;

import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.empire.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.empire.framework.mybatis.core.mapper.BaseMapperX;
import com.empire.module.member.dal.dataobject.mapconfig.MapConfigDO;
import org.apache.ibatis.annotations.Mapper;
import com.empire.module.member.controller.admin.mapconfig.vo.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 地图配置主 Mapper
 *
 * @author 主席大人
 */
@Mapper
public interface MapConfigMapper extends BaseMapperX<MapConfigDO> {

//    default PageResult<MapConfigDO> selectPage(MapConfigPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<MapConfigDO>()
//                .eqIfPresent(MapConfigDO::getCreateDeptId, reqVO.getCreateDeptId())
//                .eqIfPresent(MapConfigDO::getWarnType, reqVO.getWarnType())
//                .eqIfPresent(MapConfigDO::getFenceType, reqVO.getFenceType())
//                .inIfPresent(MapConfigDO::getId, reqVO.getIdList())
//                .and(StrUtil.isNotBlank(reqVO.getKeyword()), w -> w
//                        .like(MapConfigDO::getName, reqVO.getKeyword())
//                        .or()
//                        .like(MapConfigDO::getDescription, reqVO.getKeyword())
//                ));
//    }


    default IPage<MapConfigDO> selectPage(MapConfigPageReqVO reqVO) {
        // 创建分页对象
        Page<MapConfigDO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());

        // 获取当前时间
        Date currentTime = new Date();

        // 构建原生LambdaQueryWrapper
        LambdaQueryWrapper<MapConfigDO> queryWrapper = new LambdaQueryWrapper<>();

        // 处理createDeptId条件
        if (reqVO.getCreateDeptId() != null) {
            queryWrapper.eq(MapConfigDO::getCreateDeptId, reqVO.getCreateDeptId());
        }

        // 处理warnType条件
        if (reqVO.getWarnType() != null) {
            queryWrapper.eq(MapConfigDO::getWarnType, reqVO.getWarnType());
        }

        // 处理fenceType条件
        if (reqVO.getFenceType() != null) {
            queryWrapper.eq(MapConfigDO::getFenceType, reqVO.getFenceType());
        }

        // 处理idList条件
        if (reqVO.getIdList() != null && !reqVO.getIdList().isEmpty()) {
            queryWrapper.in(MapConfigDO::getId, reqVO.getIdList());
        }

        // 处理keyword条件
        if (StrUtil.isNotBlank(reqVO.getKeyword())) {
            queryWrapper.and(w -> w
                    .like(MapConfigDO::getName, reqVO.getKeyword())
                    .or()
                    .like(MapConfigDO::getDescription, reqVO.getKeyword())
            );
        }

        // 处理fenceStatus条件
        Integer fenceStatus = reqVO.getFenceStatus();
        if (fenceStatus != null) {
            if (fenceStatus == 1) {
                // 1-正在监控：endTime >= 当前时间 且 未删除
                queryWrapper.ge(MapConfigDO::getEndTime, currentTime)
                        .eq(MapConfigDO::getDeleted, false);
            } else if (fenceStatus == 2) {
                // 2-已经失效（时间过期）：endTime < 当前时间
                queryWrapper.lt(MapConfigDO::getEndTime, currentTime);
            } else if (fenceStatus == 3) {
                // 3-已经失效（已删除）：deleted=1
                queryWrapper.eq(MapConfigDO::getDeleted, true);
            }
        }
        queryWrapper.orderByDesc(MapConfigDO::getId);
        // 执行分页查询
        return selectPage(page, queryWrapper);
    }

    default List<MapConfigDO> getConfigSelect(WarnConfigListVo reqVO) {

        // 构建原生LambdaQueryWrapper
        LambdaQueryWrapper<MapConfigDO> queryWrapper = new LambdaQueryWrapper<>();

        // 处理createDeptId条件
        if (reqVO.getCreateDeptId() != null) {
            queryWrapper.eq(MapConfigDO::getCreateDeptId, reqVO.getCreateDeptId());
        }

        // 处理warnType条件
        if (reqVO.getWarnType() != null) {
            queryWrapper.eq(MapConfigDO::getWarnType, reqVO.getWarnType());
        }

        // 处理fenceType条件
        if (reqVO.getFenceType() != null) {
            queryWrapper.eq(MapConfigDO::getFenceType, reqVO.getFenceType());
        }

        // 处理idList条件
        if (reqVO.getIdList() != null && !reqVO.getIdList().isEmpty()) {
            queryWrapper.in(MapConfigDO::getId, reqVO.getIdList());
        }
        queryWrapper.orderByDesc(MapConfigDO::getId);
        // 执行分页查询
        return selectList(queryWrapper);
    }



}